package com.kania.simpleemail.composer;

import javax.mail.MessagingException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.kania.simpleemail.R;
import com.kania.simpleemail.core.SMTP;
import com.kania.simpleemail.data.MailData;

public class ComposerActivity extends Activity {
	
	private MailData sendMailData;
	private MailSendTask taskMailSend = null;
	
	private SMTP sendSession;
	
	private String sID = "";
	private String sPassword = "";
	private String sHost = "smtp.gmail.com";
	
	private String sTag = "KANIA_LOG_COMPOSER";
	
	
	private EditText editTo;
	private EditText editCC;
	private EditText editBCC;
	private EditText editSubject;
	private EditText editContents; //TODO 이 부분은 멀티파트로 구분되도록 한다.

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_composer);
		
		//메일 전송에 필요한 정보를 MailData 클래스로 사용하도록 한다.
		sendMailData = new MailData();
		Intent maillistIntent = getIntent();
		sID = maillistIntent.getStringExtra("ID");
		sPassword = maillistIntent.getStringExtra("password");
//		sHost = maillistIntent.getStringExtra("host");
		sendMailData.setFromData(sID);
		
		TextView textFrom = (TextView)findViewById(R.id.composerTextFrom);
		textFrom.setText(sendMailData.getFromData());
		
		Button btnSend = (Button)findViewById(R.id.composerBtnSend);
		btnSend.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//TODO send버튼의 이벤트 처리
				//메일 정보를 받아오고 부족할 시엔 토스트를 띄워 전송시키지 않는다
				//사전 처리 작업
				editTo = (EditText)findViewById(R.id.composerEditTo);
				editCC = (EditText)findViewById(R.id.composerEditCC);
				editBCC = (EditText)findViewById(R.id.composerEditBCC);
				editSubject = (EditText)findViewById(R.id.composerEditSubject);
				editContents = (EditText)findViewById(R.id.composerEditContents);
				
				if(editTo.getText().toString().equals("")) {
					Toast.makeText(ComposerActivity.this, "To. is empty!", Toast.LENGTH_SHORT).show();
				} else {
					//이 아래 5줄 부분 함수 있음... 함수로 나중에 바꾸자
					sendMailData.setToData(editTo.getText().toString());
					sendMailData.setCCData(editCC.getText().toString());
					sendMailData.setBCCData(editBCC.getText().toString());
					sendMailData.setMailSubject(editSubject.getText().toString());
					sendMailData.setContents(editContents.getText().toString());
					//날짜 데이터는 전송시 자동으로 현재 시간을 기록하도록 한다.
					Log.d(sTag, "sendMailData = \n\tfrom: " + sendMailData.getFromData() 
							+ "\n\tTo: " + sendMailData.getToData()
							+ "\n\tCC: " + sendMailData.getCCData()
							+ "\n\tBCC: " + sendMailData.getBCCData()
							+ "\n\tSubject: " + sendMailData.getMailSubject()
							+ "\n\tContents: " + sendMailData.getContents());
					taskMailSend = new MailSendTask();
					taskMailSend.execute();
					Toast.makeText(ComposerActivity.this, "Sending mail...", Toast.LENGTH_SHORT).show();
					finish();
				}
				
			}
		});
		Button btnCancel = (Button)findViewById(R.id.composerBtnCancel);
		btnCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//TODO 전송 및 드래프트 관련 처리를 넣도록 하자
				finish();
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.composer, menu);
		return true;
	}
	
	class MailSendTask extends AsyncTask<Void, Void, Boolean> {

		@Override
		protected Boolean doInBackground(Void... params) {
			sendSession = SMTP.getInstance();
			sendSession.initSMTP(sHost, sID, sPassword);
			sendSession.setSendMailData(sendMailData);
			try {
				sendSession.open();
				sendSession.send();
			} catch (MessagingException e) {
				Log.e(sTag, "MessagingException openning sendSession");
				e.printStackTrace();
			}
			return null;
		}
	}
}
